package com.axc.gallery2.dbhelper;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.os.Environment;

import com.renren.api.connect.android.photos.AlbumBean;
import com.renren.api.connect.android.photos.PhotoBean;

/**
 * 对网络相册数据库表操作辅助类
 *
 */
public class NetAlbumDHhelper extends DbHelper {

	public NetAlbumDHhelper(Context context) {
		super(context);		
	}

	/**
	 * 获取所有网络相册
	 * @return
	 */
	public List<NetAlbumInfo> getNetAlbums() {
		if(isTableExist(TABLE_NETALBUMDETAILED))
		{
		String sql = "SELECT * FROM "+TABLE_NETALBUMDETAILED;
		Cursor cursor = super.rawQuery(sql, null);
        List<NetAlbumInfo>  net_albums = new ArrayList<NetAlbumInfo>();
        int nameIndex =cursor.getColumnIndex(NAME);
        int aidIndex =cursor.getColumnIndex(AID);
        int sizeIndex =cursor.getColumnIndex(SIZE);
        for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())
        {
            int size =cursor.getInt(sizeIndex);
            String albumName = cursor.getString(nameIndex);
            String albumAID = cursor.getString(aidIndex);
            net_albums.add(new NetAlbumInfo(albumAID,albumName,size));
        };
        cursor.close();
        super.colseDBbase();
        return net_albums;
        }		
		else {
	        createTalbe(db, TABLE_NETALBUMDETAILED, new String[] {
	                AID, URL, UID, NAME, SIZE, TYPE
	        });
		}
		return new ArrayList<NetAlbumInfo>();
		
	}
	
	 /**
     * 根据相册Id获取所有网络图片
     * @return
     */
    public List<NetPhotosInfo> getNetPhotos(String aid) {
        if(isTableExist(TABLE_NETALBUMINFO))
        {
        String sql = "SELECT * FROM "+TABLE_NETALBUMINFO + " WHERE aid='"+ aid +"'";
        Cursor cursor = super.rawQuery(sql, null);
        List<NetPhotosInfo>  net_photos = new ArrayList<NetPhotosInfo>();
        int pidIndex =cursor.getColumnIndex(PID);
        int s_urlIndex =cursor.getColumnIndex(S_URL);
        int b_urlIndex =cursor.getColumnIndex(B_URL);
        int l_urlIndex = cursor.getColumnIndex(L_URL);
        for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())
        {
            String pid =cursor.getString(pidIndex);
            String s_url = cursor.getString(s_urlIndex);
            String b_url = cursor.getString(b_urlIndex);
            String l_url = cursor.getString(l_urlIndex);
            net_photos.add(new NetPhotosInfo(pid,s_url,b_url,l_url));
        };
        cursor.close();
        super.colseDBbase();
        return net_photos;
        }       
        else {
            createTalbe(db, TABLE_NETALBUMINFO, new String[] {
                    AID, PID, S_URL, B_URL, L_URL
            });
        }
        return new ArrayList<NetPhotosInfo>();
        
    }
	
	
	/**
	 * 更新相册
	 * @param aid
	 */
	public void updateNetAlbum(AlbumBean albumBean) {
		deleteNetAlbum(albumBean.getAid());
		addNetAlbum(albumBean);
	}
	/**
	 * 添加网络相册
	 * @param albumBean
	 */
	public void addNetAlbum(AlbumBean albumBean) {
	   
		insert(TABLE_NETALBUMDETAILED,  new String[]{AID,URL,UID,NAME,SIZE,TYPE},
				new String[]{String.valueOf(albumBean.getAid()),albumBean.getUrl(),String.valueOf(albumBean.getUid())
				,albumBean.getName(),String.valueOf(albumBean.getSize()),String.valueOf(albumBean.getType())});
		
	}
	
	public void addNetAlbumPhotos(PhotoBean photoBean){
	    String fileName =Environment.getExternalStorageDirectory().getPath()+"/DCIM/Gallery2/temp/_"+photoBean.getUrlTiny().split("/")[photoBean.getUrlTiny().split("/").length-1];
        insert(TABLE_NETALBUMINFO,  new String[]{AID,PID,S_URL,B_URL,L_URL}, new String[]{String.valueOf(photoBean.getAid()),
                String.valueOf(photoBean.getPid()),photoBean.getUrlTiny(),photoBean.getUrlLarge(),fileName.substring(0, fileName.length()-4)});
	}

	public void deleteNetAlbum(long aid) {
		delete(TABLE_NETALBUMDETAILED, String.valueOf(aid));
		delete(TABLE_NETALBUMINFO, String.valueOf(aid));
	}
	
	public void clearAlbum() {
	    excuteSql("delete from "+TABLE_NETALBUMINFO);
        excuteSql("delete from "+TABLE_NETALBUMDETAILED);
    }
	/**
	 * 更新网络相册图片前，先清除原来的。
	 * @param aid 
	 *         相册ID
	 */
	public void clearAlbumPhotos(long aid){
	    delete(TABLE_NETALBUMINFO, new String[]{AID}, new String[]{String.valueOf(aid)});
	}
	
}
